package com.thb.pay.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.sys.vo.req.SysUserPageReq;
import com.thb.pay.api.sys.vo.req.SysUserReq;
import com.thb.pay.api.sys.vo.resp.SysUserVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.system.entity.SysUser;
import com.thb.pay.system.entity.SysUserRole;
import com.thb.pay.system.mapper.SysUserMapper;
import com.thb.pay.system.service.SysUserRoleService;
import com.thb.pay.system.service.SysUserService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 系统-用户表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageResult<SysUserVo> findPage(SysUserPageReq pageReq) {
        IPage<SysUser> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<SysUser> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, SysUserVo.class);
    }

    @Override
    public void add(SysUserReq sysUserReq) {
        Assert.notNull(sysUserReq, "sysUserReq.not.null");
        SysUser sysUser = new SysUser();
        BeanUtil.copyProperties(sysUserReq, sysUser);
        super.insert(sysUser);
    }

    @Override
    public void update(SysUserReq sysUserReq) {
        Assert.notNull(sysUserReq, "sysUserReq.not.null");
        Assert.notNull(sysUserReq.getId(), "userId.not.null");
        SysUser sysUser = super.selectById(sysUserReq.getId());
        Assert.notNull(sysUser, "sysUser.not.exist");
        SysUser targetSysUser = new SysUser();
        BeanUtil.copyProperties(sysUserReq, targetSysUser);
        super.updateById(targetSysUser);
    }

    @Override
    public SysUserVo getByUserId(Long userId) {
        Assert.notNull(userId, "userId.not.null");
        SysUser sysUser = super.selectById(userId);
        Assert.notNull(sysUser, "sysUser.not.exist");
        SysUserVo sysUserVo = new SysUserVo();
        BeanUtil.copyProperties(sysUser, sysUserVo);
        return sysUserVo;
    }

    @Override
    public SysUserVo getByUserName(String userName) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName, userName);
        List<SysUser> sysUserList = super.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(sysUserList)) {
            SysUserVo sysUserVo = new SysUserVo();
            BeanUtil.copyProperties(sysUserList.get(0), sysUserVo);
            return sysUserVo;
        }
        return null;
    }

    @Override
    public void deleteBatchIds(List<Long> userIds) {
        Assert.notEmpty(userIds, "userIds.not.null");
        LambdaQueryWrapper<SysUserRole> queryUserRoleWrapper = new LambdaQueryWrapper<>();
        queryUserRoleWrapper.in(SysUserRole::getUserId, userIds);
        sysUserRoleService.delete(queryUserRoleWrapper);
        super.deleteBatchIds(userIds);
    }

    @Override
    public void addRoleIdListByUserId(Long userId, List<Long> roleIds) {
        Assert.notNull(userId, "userId.not.null");
        Assert.notEmpty(roleIds, "roleIds.not.null");
        LambdaQueryWrapper<SysUserRole> queryUserRoleWrapper = new LambdaQueryWrapper<>();
        queryUserRoleWrapper.in(SysUserRole::getUserId, userId);
        sysUserRoleService.delete(queryUserRoleWrapper);
        List<SysUserRole> sysUserRoleList = Lists.newArrayList();
        roleIds.forEach(roleId -> {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            sysUserRoleList.add(sysUserRole);
        });
        sysUserRoleService.insertBatch(sysUserRoleList, 15);
    }
}
